package com.cloud.sso.common.config;

import com.cloud.sso.oauth.handler.AppLoginFailureHandler;
import com.cloud.sso.oauth.handler.AppLoginSuccessHandler;
import com.cloud.sso.oauth.service.AppUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;

@Order
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AppUserDetailService appUserDetailService;

    @Autowired
    private AppLoginSuccessHandler appLoginSuccessHandler;

    @Autowired
    private AppLoginFailureHandler appLoginFailureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.requestMatchers()
                .antMatchers("/login")
                .antMatchers("/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .antMatchers("/oauth/token").permitAll()
                .and()
                .formLogin().loginPage("/login").permitAll()    // 自定义登录页面，这里配置了 loginPage, 就会通过 LoginController 的 login 接口加载登录页面
                .and().csrf().disable();
        http.formLogin()
                .successHandler(appLoginSuccessHandler)
                .failureHandler(appLoginFailureHandler);
/*
        http.csrf().disable()
                .requestMatchers()
                .anyRequest()
                .and()
                .authorizeRequests()
                .antMatchers("/login/**", "/oauth/**")
                .permitAll()
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .logout()
                .clearAuthentication(true)
                .invalidateHttpSession(true);
        ;

        http.formLogin().loginPage("http://127.0.0.1:9527/login")
                .successHandler(appLoginSuccessHandler)
                .failureHandler(appLoginFailureHandler);*/

        http.httpBasic().disable();
        http.sessionManagement().invalidSessionUrl("/login");

        http.sessionManagement().maximumSessions(100).maxSessionsPreventsLogin(true);

       /* http.csrf().disable().requestMatchers()
                .anyRequest()
                .and()
                .authorizeRequests()
                .antMatchers("/login", "/loginPage")
                .permitAll()
                .antMatchers("/logout")
                .permitAll()
                .antMatchers("/oauth/**")
                .permitAll()
                .anyRequest().authenticated()
                .and()
                .logout()
                .clearAuthentication(true)
                .invalidateHttpSession(true);

        // 自定义登录
        http.formLogin().loginPage("/loginPage")
                .successHandler(appLoginSuccessHandler)
                .failureHandler(appLoginFailureHandler);

        http.httpBasic().disable();
        http.sessionManagement().invalidSessionUrl("/login");

        http.sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true);*/

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(appUserDetailService).passwordEncoder(passwordEncoder());
/*
auth.inMemoryAuthentication().passwordEncoder(passwordEncoder())
                .withUser("admin").password(passwordEncoder().encode("123456")).roles("ADMIN");
*/

    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }



    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("123123"));
    }
}

